package org.linlinjava.litemall.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.linlinjava.litemall.db.dao.AfterSaleMapper;
import org.linlinjava.litemall.db.models.AfterSale;
import org.linlinjava.litemall.db.service.AfterSaleService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class AfterSaleServiceImpl extends ServiceImpl<AfterSaleMapper, AfterSale>
        implements AfterSaleService {

    @Override
    public AfterSale findById(Long userId, Long id) {
        return this.baseMapper.selectOne(new LambdaQueryWrapper<AfterSale>()
                .eq(AfterSale::getId, id)
                .eq(AfterSale::getUserId, userId));
    }

    @Override
    public IPage<AfterSale> queryList(Long userId, Short status, Integer pageNo, Integer pageSize) {
        Page<AfterSale> page = new Page<>(pageNo, pageSize);
        QueryWrapper<AfterSale> query = new QueryWrapper<>();
        query.eq("user_id", userId);
        query.eq(status != null, "status", status);
        query.orderByDesc("id");
        return baseMapper.selectPage(page, query);
    }

    @Override
    public IPage<AfterSale> querySelective(Long orderId, String afterSaleSn, Short status, Integer pageNo, Integer pageSize) {
        Page<AfterSale> page = new Page<>(pageNo, pageSize);
        QueryWrapper<AfterSale> query = new QueryWrapper<>();
        query.eq(orderId != null, "order_id", orderId);
        query.eq(!StringUtils.isEmpty(afterSaleSn), "aftersale_sn", afterSaleSn);
        query.eq(status != null, "status", status);
        query.orderByDesc("id");
        return baseMapper.selectPage(page, query);
    }

    @Override
    public int countByAfterSaleSn(Long userId, String afterSaleSn) {
        QueryWrapper<AfterSale> query = new QueryWrapper<>();
        query.eq("user_id", userId).eq("aftersale_sn", afterSaleSn);
        return baseMapper.selectCount(query);
    }

    @Override
    public String generateAfterSaleSn(Long userId) {
        return IdWorker.getTimeId();
    }

    @Override
    public void deleteByOrderId(Long userId, Long orderId) {
        Wrapper<AfterSale> query = new LambdaQueryWrapper<AfterSale>()
                .eq(AfterSale::getUserId, userId)
                .eq(AfterSale::getOrderId, orderId);
        baseMapper.delete(query);
    }

    @Override
    public AfterSale findByOrderId(Long userId, Long orderId) {
        return this.baseMapper.selectOne(new LambdaQueryWrapper<AfterSale>()
                .eq(AfterSale::getUserId, userId)
                .eq(AfterSale::getOrderId, orderId));
    }
}
